/**
 * 上传管理器 - 处理图片上传和数据更新
 */
class UploadManager {
  constructor() {
    this.apiBaseUrl = window.location.protocol + '//' + window.location.hostname + ':3000/api';
  }

  /**
   * 上传图片
   * @param {File} file - 要上传的图片文件
   * @param {string} type - 图片类型（albums, diary, memory, moments, couples）
   * @returns {Promise<Object>} - 包含上传成功信息和文件路径的Promise
   */
  async uploadImage(file, type) {
    try {
      const formData = new FormData();
      formData.append('image', file);
      formData.append('type', type);

      const response = await fetch(`${this.apiBaseUrl}/upload`, {
        method: 'POST',
        body: formData,
      });

      if (!response.ok) {
        const errorData = await response.json();
        throw new Error(errorData.message || '上传图片失败');
      }

      return await response.json();
    } catch (error) {
      console.error('上传图片时出错:', error);
      throw error;
    }
  }

  /**
   * 添加新相册
   * @param {Object} albumData - 相册数据
   * @returns {Promise<Object>} - 添加结果
   */
  async addAlbum(albumData) {
    try {
      const response = await fetch(`${this.apiBaseUrl}/albums/add`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(albumData),
      });

      if (!response.ok) {
        const errorData = await response.json();
        throw new Error(errorData.message || '添加相册失败');
      }

      return await response.json();
    } catch (error) {
      console.error('添加相册时出错:', error);
      throw error;
    }
  }

  /**
   * 删除相册
   * @param {number} albumId - 要删除的相册ID
   * @returns {Promise<Object>} - 删除结果
   */
  async deleteAlbum(albumId) {
    try {
      const response = await fetch(`${this.apiBaseUrl}/albums/${albumId}`, {
        method: 'DELETE',
      });

      if (!response.ok) {
        const errorData = await response.json();
        throw new Error(errorData.message || '删除相册失败');
      }

      return await response.json();
    } catch (error) {
      console.error('删除相册时出错:', error);
      throw error;
    }
  }

  /**
   * 添加新日志
   * @param {Object} diaryData - 日志数据
   * @returns {Promise<Object>} - 添加结果
   */
  async addDiary(diaryData) {
    try {
      const response = await fetch(`${this.apiBaseUrl}/diary/add`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(diaryData),
      });

      if (!response.ok) {
        const errorData = await response.json();
        throw new Error(errorData.message || '添加日志失败');
      }

      return await response.json();
    } catch (error) {
      console.error('添加日志时出错:', error);
      throw error;
    }
  }

  /**
   * 删除日志
   * @param {number} diaryId - 要删除的日志ID
   * @returns {Promise<Object>} - 删除结果
   */
  async deleteDiary(diaryId) {
    try {
      const response = await fetch(`${this.apiBaseUrl}/diary/${diaryId}`, {
        method: 'DELETE',
      });

      if (!response.ok) {
        const errorData = await response.json();
        throw new Error(errorData.message || '删除日志失败');
      }

      return await response.json();
    } catch (error) {
      console.error('删除日志时出错:', error);
      throw error;
    }
  }

  /**
   * 添加新纪念日
   * @param {Object} memoryData - 纪念日数据
   * @returns {Promise<Object>} - 添加结果
   */
  async addMemory(memoryData) {
    try {
      const response = await fetch(`${this.apiBaseUrl}/memory/add`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(memoryData),
      });

      if (!response.ok) {
        const errorData = await response.json();
        throw new Error(errorData.message || '添加纪念日失败');
      }

      return await response.json();
    } catch (error) {
      console.error('添加纪念日时出错:', error);
      throw error;
    }
  }

  /**
   * 删除纪念日
   * @param {number} memoryId - 要删除的纪念日ID
   * @returns {Promise<Object>} - 删除结果
   */
  async deleteMemory(memoryId) {
    try {
      const response = await fetch(`${this.apiBaseUrl}/memory/${memoryId}`, {
        method: 'DELETE',
      });

      if (!response.ok) {
        const errorData = await response.json();
        throw new Error(errorData.message || '删除纪念日失败');
      }

      return await response.json();
    } catch (error) {
      console.error('删除纪念日时出错:', error);
      throw error;
    }
  }
}

// 导出上传管理器
window.UploadManager = UploadManager; 